package com.weihui.jxc.view.goods;

import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.common.ValidationManager;
import com.weihui.jxc.model.Customer;
import com.weihui.jxc.model.Goods;
import com.weihui.jxc.model.Item;
import com.weihui.jxc.model.Provider;
import com.weihui.jxc.service.GoodsService;
import com.weihui.jxc.service.ProviderService;
import com.weihui.jxc.view.CustomerInternalFrame;
import com.weihui.jxc.view.GoodsInternalFrame;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;

/**
 * 商品信息添加面板
 * 
 * @author MXH
 *
 */
public class GoodsAddPanel extends JPanel implements ActionListener {
	private JTextField nameTf;
	private JTextField shortTf;
	private JTextField placeTf;
	private JTextField sizeTf_1;
	private JTextField packageTf;
	private JTextField productCodeTf;
	private JTextField pCodeTf;
	private JTextField priceTf;
	private JTextField memoTf;
    private JComboBox comboBox;
    private ProviderService providerService;
    private GoodsService goodsService;
    private Item selectedItem;//封装供应商编号和对应的全称
	/**
	 * Create the panel.
	 */
	public GoodsAddPanel() {

		JPanel panel = new JPanel();

		JPanel panel_1 = new JPanel();

		shortTf = new JTextField();
		shortTf.setColumns(20);

		JLabel label = new JLabel("商品简称：");
		label.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/商品简称.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 522, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup().addContainerGap().addComponent(label)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(shortTf, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE).addContainerGap()));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 49, Short.MAX_VALUE)
				.addGroup(gl_panel_1.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE).addComponent(label).addComponent(
								shortTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
						.addContainerGap(24, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JPanel panel_2 = new JPanel();

		placeTf = new JTextField();
		placeTf.setColumns(20);

		JLabel label_1 = new JLabel("商品产地：");
		label_1.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/地址.png")));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING).addGap(0, 522, Short.MAX_VALUE)
				.addGroup(gl_panel_2.createSequentialGroup().addContainerGap().addComponent(label_1)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(placeTf, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE).addContainerGap()));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING).addGap(0, 49, Short.MAX_VALUE)
				.addGroup(gl_panel_2.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE).addComponent(label_1).addComponent(
								placeTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
						.addContainerGap(24, Short.MAX_VALUE)));
		panel_2.setLayout(gl_panel_2);

		JPanel panel_3 = new JPanel();

		sizeTf_1 = new JTextField();
		sizeTf_1.setColumns(20);

		JLabel sizeTf = new JLabel("商品规格：");
		sizeTf.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/运-规格.png")));

		JLabel label_2 = new JLabel("商品包装：");
		label_2.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/包装.png")));

		packageTf = new JTextField();
		packageTf.setColumns(20);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addComponent(sizeTf)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(sizeTf_1, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(packageTf, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(103, Short.MAX_VALUE))
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(sizeTf)
						.addComponent(sizeTf_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2)
						.addComponent(packageTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(15, Short.MAX_VALUE))
		);
		panel_3.setLayout(gl_panel_3);

		JPanel panel_4 = new JPanel();

		JLabel label_3 = new JLabel("商品批号：");
		label_3.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/批号.png")));

		productCodeTf = new JTextField();
		productCodeTf.setColumns(20);

		JLabel label_4 = new JLabel("批准文号：");
		label_4.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/批准文号.png")));

		pCodeTf = new JTextField();
		pCodeTf.setColumns(20);
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addComponent(label_3)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(productCodeTf, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(pCodeTf, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(70, Short.MAX_VALUE))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_4)
							.addComponent(pCodeTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_3)
							.addComponent(productCodeTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(15, Short.MAX_VALUE))
		);
		panel_4.setLayout(gl_panel_4);

		JPanel panel_5 = new JPanel();

		JLabel label_5 = new JLabel("商品价格：");
		label_5.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/价格.png")));

		priceTf = new JTextField();
		priceTf.setColumns(20);
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(gl_panel_5.createParallelGroup(Alignment.LEADING).addGap(0, 522, Short.MAX_VALUE)
				.addGroup(gl_panel_5.createSequentialGroup().addContainerGap().addComponent(label_5)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(priceTf, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE).addContainerGap()));
		gl_panel_5.setVerticalGroup(gl_panel_5.createParallelGroup(Alignment.LEADING).addGap(0, 49, Short.MAX_VALUE)
				.addGroup(gl_panel_5.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel_5.createParallelGroup(Alignment.BASELINE).addComponent(label_5).addComponent(
								priceTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		panel_5.setLayout(gl_panel_5);

		JPanel panel_6 = new JPanel();

		JLabel label_6 = new JLabel("商品备注：");
		label_6.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/备注.png")));

		memoTf = new JTextField();
		memoTf.setColumns(20);
		GroupLayout gl_panel_6 = new GroupLayout(panel_6);
		gl_panel_6.setHorizontalGroup(gl_panel_6.createParallelGroup(Alignment.LEADING).addGap(0, 522, Short.MAX_VALUE)
				.addGroup(gl_panel_6.createSequentialGroup().addContainerGap().addComponent(label_6)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(memoTf, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE).addContainerGap()));
		gl_panel_6.setVerticalGroup(gl_panel_6.createParallelGroup(Alignment.LEADING).addGap(0, 49, Short.MAX_VALUE)
				.addGroup(gl_panel_6.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel_6.createParallelGroup(Alignment.BASELINE).addComponent(label_6).addComponent(
								memoTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		panel_6.setLayout(gl_panel_6);

		JPanel panel_7 = new JPanel();

		JLabel label_7 = new JLabel("供应商全称：");

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				selectAction();
			}
		});
		GroupLayout gl_panel_7 = new GroupLayout(panel_7);
		gl_panel_7.setHorizontalGroup(gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_7.createSequentialGroup().addContainerGap().addComponent(label_7)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(comboBox, 0, 398, Short.MAX_VALUE)
						.addContainerGap()));
		gl_panel_7.setVerticalGroup(gl_panel_7.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_7
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel_7.createParallelGroup(Alignment.BASELINE).addComponent(label_7).addComponent(
						comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		panel_7.setLayout(gl_panel_7);

		JPanel panel_8 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(groupLayout
				.createSequentialGroup().addContainerGap()
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_8, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_6, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
						.addComponent(panel_7, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE))
				.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_7, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(panel_8, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(22, Short.MAX_VALUE)));

		JButton saveBtn = new JButton("保存");
		saveBtn.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/login.png")));
		// 给保存绑定点击事件
		saveBtn.addActionListener(this);
		panel_8.add(saveBtn);

		JButton resetBtn = new JButton("重置");
		resetBtn.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/重置.png")));
		// 给重置绑定点击事件
		resetBtn.addActionListener(this);
		panel_8.add(resetBtn);

		JLabel lblNewLabel = new JLabel("商品名称：");
		lblNewLabel.setIcon(new ImageIcon(GoodsAddPanel.class.getResource("/images/商品名称.png")));

		nameTf = new JTextField();
		nameTf.setColumns(20);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup().addContainerGap().addComponent(lblNewLabel)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(nameTf, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel).addComponent(
						nameTf, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(24, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		setLayout(groupLayout);
		setBounds(10, 10, 587, 511);
        //实例化供应商service
		providerService = CommonFactory.getProviderService();
		//实例化商品service
		goodsService = CommonFactory.getGoodsService();
		//初始化供应商下拉列表框
		initComboBox();
	}
	/**
	 * 初始供应商下拉选择框
	 */
	public void initComboBox() {
		//获取数据库中的数据
		List<Provider> list = providerService.findProviders(null);
		//设置下来列表框中数据模型
		Vector<Item> items = new Vector<>();
		//清空下拉列表框中的内容，免的重复
		comboBox.removeAllItems();
		//迭代处理数据
		for(int i=0;i<list.size();i++) {
			Item item = new Item();
			item.setId(list.get(i).getId());//数据库要用
			item.setName(list.get(i).getCustomerName());//界面展示用
			if(items.contains(item)) {//如果集合中包含该元素，就不在添加
				continue;			
			}
			items.add(item);	
			comboBox.addItem(item);
		}
	}
	/**
	 * 下拉列表框点击事件
	 */
	private void selectAction() {
	
		if(!(comboBox.getSelectedItem() instanceof Item)) {
			return;
		}
		//每选择一次，就更新一次Item
		selectedItem = (Item) comboBox.getSelectedItem();
		System.out.println(selectedItem.getId()+","+selectedItem.getName());		
	}
	/**
	 * 点击事件
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
		case "保存":
            //非空验证
			Boolean isTrue = GoodsInternalFrame.dataCheck(nameTf.getText().trim(), shortTf.getText().trim(), 
					placeTf.getText().trim(),sizeTf_1.getText().trim() , 
					packageTf.getText().trim(), productCodeTf.getText().trim(), 
					pCodeTf.getText().trim(), priceTf.getText().trim(), 
					memoTf.getText().trim(), selectedItem.getName().trim());
			
			if(isTrue) {
				//保存数据
			Goods goods = new Goods(goodsService.getGoodsId(), 
					nameTf.getText().trim(), shortTf.getText().trim(), 
					placeTf.getText().trim(), sizeTf_1.getText().trim(), 
					packageTf.getText().trim(), productCodeTf.getText().trim(),
					pCodeTf.getText().trim(), Float.parseFloat(priceTf.getText().trim()),
					memoTf.getText().trim(), selectedItem.getId());
			boolean isFlag = goodsService.saveGoods(goods);
			if(isFlag==true) {
				JOptionPane.showMessageDialog(null, "商品信息添加成功！","提示",JOptionPane.INFORMATION_MESSAGE);
				setNull();
			}else {
				JOptionPane.showMessageDialog(null, "商品信息添加失败！","警告",JOptionPane.WARNING_MESSAGE);			
			}
			}else {
				JOptionPane.showMessageDialog(null, "填写的数据非法，请认真检查！","警告",JOptionPane.WARNING_MESSAGE);
			}
			
			break;
		case "重置":

			break;
		default:
			break;
		}
	}
	/**
	 * 清空输入框
	 */
	public void setNull() {
		nameTf.setText("");
		shortTf.setText("");
		placeTf.setText("");
		sizeTf_1.setText(""); 
		packageTf.setText("");
		productCodeTf.setText("");
		pCodeTf.setText("");
		priceTf.setText("");
		memoTf.setText("");
	}
}